package com.drinks.yiniang.ui.activity.goods;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.goods.RecommendGoodsAdapter;
import com.drinks.yiniang.common.recycler.decoration.RecommendGoodsItemDecoration;
import com.drinks.yiniang.entity.event.ShopCartEvent;
import com.drinks.yiniang.entity.goods.GoodsJson;
import com.drinks.yiniang.entity.goods.RecommendGoodsJson;
import com.drinks.yiniang.io.api.GoodsApiIO;
import com.drinks.yiniang.io.api.ShoppingCartApiIO;
import com.drinks.yiniang.ui.activity.message.MessageActivity;
import com.drinks.yiniang.ui.widget.nav.NavigationSearchBarLayout;
import com.drinks.yiniang.ui.widget.recycler.BaseRecyclerView;
import com.drinks.yiniang.utils.ActivityUtils;
import com.drinks.yiniang.utils.RecyclerUtils;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.entity.BaseJson;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.utils.CommonUtils;
import com.hansen.library.utils.StringUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

/**
 * 智能推荐商品页面
 *
 * @author han
 * @date 2019/09/08
 */
public class RecommendGoodsActivity extends BaseTranBarActivity implements OnNavgationBarClickListener,
        BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.OnItemChildClickListener {

    //nav
    private NavigationSearchBarLayout nav_bar_recommend_goods;
    private BaseRecyclerView rcv_recommend_goods;
    //添加到购物车
    private Button btn_recommend_goods_add;

    private RecommendGoodsAdapter mGoodsAdapter;

    @Override
    protected void initView() {
        nav_bar_recommend_goods = findViewById(R.id.nav_bar_recommend_goods);

        rcv_recommend_goods = findViewById(R.id.rcv_recommend_goods);

        btn_recommend_goods_add = findViewById(R.id.btn_recommend_goods_add);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;

        initAdapter();

        getRecommendGoodsList();
    }

    private void initAdapter() {
        rcv_recommend_goods.setLayoutManager(RecyclerUtils.getVerticalGridLayout(mContext, 2));
        rcv_recommend_goods.addItemDecoration(RecommendGoodsItemDecoration.getItemDPDecoration(10));

        mGoodsAdapter = new RecommendGoodsAdapter(mContext, new ArrayList<GoodsJson.GoodsList>());
        mGoodsAdapter.addHeaderView(getHeaderView());
        mGoodsAdapter.addFooterView(getFooterView());
        mGoodsAdapter.setHeaderFooterEmpty(true, true);
        mGoodsAdapter.setCommonEmpty(getLayoutInflater(), rcv_recommend_goods);
        mGoodsAdapter.bindToRecyclerView(rcv_recommend_goods);
    }

    //获取header
    private View getHeaderView() {
        View mHeaderView = getLayoutInflater().inflate(R.layout.header_common_title, (ViewGroup) rcv_recommend_goods.getParent(), false);

        TextView tvTitle = mHeaderView.findViewById(R.id.tv_header_common_title);

        tvTitle.setText(R.string.text_recommend_goods_dot);

        return mHeaderView;
    }

    //获取header
    private View getFooterView() {
        return getLayoutInflater().inflate(R.layout.footer_space76_view, (ViewGroup) rcv_recommend_goods.getParent(), false);
    }

    @Override
    protected void initListener() {
        nav_bar_recommend_goods.setOnNavgationBarClickListener(this);
        btn_recommend_goods_add.setOnClickListener(this);
        mGoodsAdapter.setOnItemClickListener(this);
        mGoodsAdapter.setOnItemChildClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        if (v.getId() == R.id.btn_recommend_goods_add) {//全部添加到购物车
            addAllToCart();
        }
    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_recommend_goods;
    }

    @Override
    public void onBackClick(View v) {
        finish();
    }

    @Override
    public void onEditClick(View v) {
        Intent intent = new Intent(mContext, MessageActivity.class);
        startActivity(intent);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        GoodsJson.GoodsList data = mGoodsAdapter.getItem(position);

        if (data == null) {
            return;
        }

        Intent intent = new Intent(mContext, GoodsDetailsActivity.class);
        intent.putExtra(Constants.KeyGoodsId, data.getId());
        startActivity(intent);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        GoodsJson.GoodsList data = mGoodsAdapter.getItem(position);

        if (data == null) {
            ToastUtils.showShort(R.string.text_the_goods_does_not_exist);
            return;
        }
        switch (view.getId()) {
            case R.id.btn_recommend_goods_not_show_again://不再显示
                doNotShowAgain(data.getId(), position);
                break;
            case R.id.iv_recommend_goods_add_cart://添加到购物车
                addToCart(data.getId());
                break;
            default:
                break;
        }
    }

    private void addToCart(String goodsId) {
        if (!ActivityUtils.isLogined(this, true)) {
            return;
        }

        showLoadingDialog();

        ShoppingCartApiIO.getInstance().addToShopCart(goodsId, new APIRequestCallback<BaseJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(BaseJson res) {
                ToastUtils.showShort(getString(R.string.text_add_success));
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    //加入购物车
    private void addAllToCart() {
        if (!ActivityUtils.isLogined(this, true)) {
            return;
        }

        if (CommonUtils.isEmptyList(mGoodsAdapter.getData())) {
            return;
        }

        showLoadingDialog();

        StringBuilder builder = new StringBuilder();

        GoodsJson.GoodsList itemData;

        for (int i = 0; i < mGoodsAdapter.getData().size(); i++) {
            itemData = mGoodsAdapter.getData().get(i);

            if (i == (mGoodsAdapter.getData().size() - 1)) {
                builder.append(itemData.getId());
            } else {
                builder.append(itemData.getId());
                builder.append(",");
            }
        }

        ShoppingCartApiIO.getInstance().addRecommendGoodsToShopCart(builder.toString(), new APIRequestCallback<BaseJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(BaseJson res) {
                //更新购物车
                EventBus.getDefault().post(new ShopCartEvent());
                ToastUtils.showShort(R.string.text_add_success);
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    //不再显示
    private void doNotShowAgain(String id, final int position) {
        if (!ActivityUtils.isLogined(this, true)) {
            return;
        }

        showLoadingDialog();

        GoodsApiIO.getInstance().doGoodsNotShowAgain(id, new APIRequestCallback<BaseJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(BaseJson res) {
                if (mGoodsAdapter.getItem(position) == null) {
                    return;
                }

                mGoodsAdapter.remove(position);
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    //获取推荐列表
    private void getRecommendGoodsList() {
        showLoadingDialog();

        GoodsApiIO.getInstance().getRecommendGoodsList(new APIRequestCallback<RecommendGoodsJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(RecommendGoodsJson res) {
                if (mGoodsAdapter == null) {
                    return;
                }

                mGoodsAdapter.getData().clear();
                mGoodsAdapter.addData(res.getData());
                mGoodsAdapter.loadMoreEnd();
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
                mGoodsAdapter.loadMoreFail();
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }
}
